package com.babster631;

import java.util.ArrayList;

import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.projectile.EntityThrowable;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.DamageSource;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.EnderTeleportEvent;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class EntityThunderPearl extends EntityThrowable
{
	public EntityPlayer User;
    public EntityThunderPearl(World par1World)
    {
        super(par1World);
    }

    public EntityThunderPearl(World par1World, EntityLiving par2EntityLiving)
    {
        super(par1World, par2EntityLiving);
    }

    @SideOnly(Side.CLIENT)
    public EntityThunderPearl(World par1World, double par2, double par4, double par6)
    {
        super(par1World, par2, par4, par6);
    }

    /**
     * Called when this EntityThrowable hits a block or entity.
     */
    protected void onImpact(MovingObjectPosition par1MovingObjectPosition)
    {
    	
        if (!this.worldObj.isRemote)
        {
           
        	//this.worldObj.createExplosion()
            //this.setDead();
        	ArrayList<EntityLiving> allLiving = new ArrayList<EntityLiving>();
        	for (Object living : this.worldObj.loadedEntityList)
        	{
        		try 
        		{
        			EntityLiving _livingEntity = (EntityLiving)living;
        			if (_livingEntity == User)
        				continue;
        			if (this.getPosition().distanceTo(_livingEntity.getPosition(1)) < 10)
        			{
        				allLiving.add(_livingEntity);
        			}
        		}
        		catch(Exception e)
        		{
        			
        		}
        	}
        	while (allLiving.size() > 0)
        		//This knocks back all the mobs in the vicinity of the thunderpearl
        	{
        		Vec3 diffVector = this.getPosition().subtract(allLiving.get(0).getPosition(1));
        		diffVector = diffVector.normalize();
        		allLiving.get(0).setVelocity(diffVector.xCoord* 5, diffVector.yCoord * 1, diffVector.zCoord * 5);
        		allLiving.remove(0);
        	}
        	//this adds a healing effect to the player because of the damage he takes from the teleport
            User.addPotionEffect(new PotionEffect(Potion.regeneration.id, 7, 10, false));
        	//Vec3 knockBackVector = 
        }
    }
    protected Vec3 getPosition()
    {
    	return Vec3.createVectorHelper(posX, posY, posZ);
    }
    
}
